package com.cskaoyan.catmall.config;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 保证的是跨域场景下session变化问题
 * @author stone
 * @date 2022/01/10 11:42
 */
@Component
public class ShiroCustomSessionManager extends DefaultWebSessionManager {

    //根据特定的请求头拿到对应的sessionid信息
    private static String Authen_Token = "X-CskaoyanMarket-Admin-Token";
    private static String Authen_User_Token = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest req, ServletResponse resp) {
        HttpServletRequest request = (HttpServletRequest) req;
        String requestType = request.getRequestURI();
        if (requestType.contains("wx")){
            String sessionId = request.getHeader(Authen_User_Token);
            if (sessionId != null && !"".equals(sessionId)) {
                return sessionId;
            }
        }else if (requestType.contains("admin")){
            String sessionId = request.getHeader(Authen_Token);
            if (sessionId != null && !"".equals(sessionId)) {
                return sessionId;
            }
        }

        return super.getSessionId(req, resp);
    }
}
